﻿using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-质量管理-待检任务
    /// </summary>
    public class PendingtasksService : ApplicationService, IPendingtasksService
    {
        private readonly IRepository<PendingtasksModel, int> _pendingtasksRepository;
        private readonly IRepository<Process, int> _processRepository;

        public PendingtasksService(IRepository<PendingtasksModel, int> pendingtasksRepository, IRepository<Process, int> processRepository)
        {
            _pendingtasksRepository = pendingtasksRepository;
            _processRepository = processRepository;
        }

        /// <summary>
        /// 获取待检任务列表（带分页）
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns></returns>
        public async Task<PendingtasksPageResultDTO> GetPendingtasksPageList(PendingtasksQueryDTO query)
        {
            var queryable = await _pendingtasksRepository.GetQueryableAsync();
            
            // 构建查询条件
            var filteredQuery = queryable
                .WhereIf(!string.IsNullOrEmpty(query.FormCode), m => m.FormCode.Contains(query.FormCode))
                .WhereIf(!string.IsNullOrEmpty(query.MaterialName), m => m.MaterialName.Contains(query.MaterialName))
                .WhereIf(!string.IsNullOrEmpty(query.TestingItems), m => m.TestingItems.Contains(query.TestingItems))
                .OrderByDescending(m => m.Id);

            // 获取总数
            var totalCount = filteredQuery.Count();

            // 分页查询
            var pagedData = filteredQuery
                .Skip((query.Page - 1) * query.Limit)
                .Take(query.Limit)
                .ToList();

            // 转换为DTO
            var dtoData = pagedData.Select(m => new PendingtasksDTO
            {
                Id = m.Id,
                FormCode = m.FormCode,
                TestingItems = m.TestingItems,
                MaterialCode = m.MaterialCode,
                MaterialName = m.MaterialName,
                PendingNum = m.PendingNum,
                Position = m.Position
            }).ToList();

            return new PendingtasksPageResultDTO
            {
                Code = 0,
                Msg = "success",
                Count = totalCount,
                Data = dtoData
            };
        }

        /// <summary>
        /// 获取待检任务列表
        /// </summary>
        /// <param name="formCode">单据编码</param>
        /// <param name="materialName">物资名称</param>
        /// <param name="testingItems">检测类型</param>
        /// <returns></returns>
        public async Task<List<PendingtasksModel>> GetPendingtasksList(string? formCode, string? materialName, string? testingItems)
        {
            var queryable = await _pendingtasksRepository.GetQueryableAsync();
            var pendingtasksList = queryable
                .WhereIf(!string.IsNullOrEmpty(formCode), m => m.FormCode.Contains(formCode))
                .WhereIf(!string.IsNullOrEmpty(materialName), m => m.MaterialName.Contains(materialName))
                .WhereIf(!string.IsNullOrEmpty(testingItems), m => m.TestingItems.Contains(testingItems))
                .OrderByDescending(m => m.Id)
                .ToList();
            return pendingtasksList;
        }

        /// <summary>
        /// 添加过程检验记录
        /// </summary>
        /// <param name="processDto">过程检验数据</param>
        /// <returns></returns>
        public async Task<bool> AddProcess(ProcessDTO processDto)
        {
            try
            {
                var process = new Process
                {
                    ProcessCode = processDto.ProcessCode,
                    ProcessType = processDto.ProcessType,
                    OrderCode = processDto.OrderCode,
                    ProductCode = processDto.ProductCode,
                    ProductName = processDto.ProductName,
                    Specifications = processDto.Specifications,
                    Unitt = processDto.Unitt,
                    TestingNum = processDto.TestingNum,
                    TestingResult = processDto.TestingResult,
                    TestingDate = processDto.TestingDate,
                    TestingPerson = processDto.TestingPerson,
                    State = processDto.State
                };

                await _processRepository.InsertAsync(process);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
